package cn.zlb.frame.application.api.pay;

import cn.zlb.frame.api.pay.dto.PayReqDTO;
import cn.zlb.frame.api.pay.dto.PayRespDTO;
import cn.zlb.frame.api.pay.facade.IPayFacade;
import cn.zlb.frame.application.repository.IMerchantRepository;
import cn.zlb.frame.common.exception.BizException;
import cn.zlb.frame.common.result.Result;
import cn.zlb.frame.common.result.ResultBuilder;
import cn.zlb.frame.domain.pay.model.PayAggregateRoot;
import cn.zlb.frame.domain.pay.model.entity.PaymentEntity;
import cn.zlb.frame.domain.pay.model.entity.ProductEntity;
import cn.zlb.frame.domain.pay.model.valueobj.PayTypeValueObj;
import cn.zlb.frame.domain.pay.service.IPayDomainService;
import javax.annotation.Resource;
import org.apache.dubbo.common.constants.ClusterRules;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * @author yangjie.deng@resico.cn
 * @date 2023-11-09 11:10:56
 * @describe 获取支付信息
 */
@DubboService(interfaceClass = IPayFacade.class,
              version = "v1.0.0",
              cluster = ClusterRules.FAIL_OVER,
              loadbalance = "leastactive")
public class PayFacade implements IPayFacade {

    @Resource
    private IMerchantRepository merchantRepository;

    @Resource
    private IPayDomainService payDomainService;

    @Override
    public Result<PayRespDTO> personPay(PayReqDTO reqDTO) {
        // 获取校验
        final Result<Boolean> result = merchantRepository.merchantVerify(reqDTO.getTenantNo());
        if (!result.getData()) {
            throw new BizException(94000, "租户信息校验未通过,错误信息:[".concat(result.getMessage()).concat("]."));
        }
        PayAggregateRoot aggregateRoot = buildAggregateRoot(reqDTO);
        // 发起支付
        final PaymentEntity payResult = payDomainService.pay(aggregateRoot);
        // 返回
        return ResultBuilder.success(this.buildResponse(reqDTO, payResult));
    }

    private PayRespDTO buildResponse(PayReqDTO reqDTO, PaymentEntity payResult) {
        PayRespDTO payRespDTO = new PayRespDTO();
        payRespDTO.setTenantNo(reqDTO.getTenantNo());
        payRespDTO.setPayAmt(reqDTO.getPayAmt());
        payRespDTO.setPayType(reqDTO.getPayType());
        payRespDTO.setUniqueNo(reqDTO.getUniqueNo());
        payRespDTO.setPaymentNo(payResult.getPaymentNo());
        return payRespDTO;
    }

    private PayAggregateRoot buildAggregateRoot(PayReqDTO reqDTO) {
        PayAggregateRoot aggregateRoot = new PayAggregateRoot();
        aggregateRoot.setOrderNo(reqDTO.getOrderNo());

        final PaymentEntity payment = new PaymentEntity();
        payment.setPayType(PayTypeValueObj.findValue(reqDTO.getPayType()));
        payment.setOrderNo(reqDTO.getOrderNo());
        payment.setUserNo(reqDTO.getUserNo());
        payment.setTenantNo(reqDTO.getTenantNo());
        payment.setPayAmt(reqDTO.getPayAmt());
        payment.setProductNo(reqDTO.getProductNo());
        payment.setProductName(reqDTO.getProductName());
        aggregateRoot.setPaymentEntity(payment);


        final ProductEntity productEntity = new ProductEntity();
        productEntity.setProductNo(reqDTO.getProductNo());
        productEntity.setTenantNo(reqDTO.getTenantNo());
        productEntity.setInventory(1);
        productEntity.setUserNo(reqDTO.getUserNo());
        aggregateRoot.setProductEntity(productEntity);
        return aggregateRoot;
    }
}
